package com.gzz.orchid.context.infrastructure.definition;

import com.gzz.orchid.common.core.support.IBaseEnum;

import java.util.HashSet;
import java.util.Set;

/**
 * 系统操作
 *
 * @author higmd
 */
public enum Action implements IBaseEnum {
    // 查看
    VIEW(1, "action.view"),
    // 新增
    ADD(2, "action.add"),
    // 修改
    EDIT(4, "action.edit"),
    // 删除
    DELETE(8, "action.delete"),
    // 审核
    REVIEW(16, "action.audit"),
    // 取消
    CANCEL(32, "action.cancel"),
    //  审批
    APPROVE(64, "action.approve"),
    // 导入
    IMPORT(128, "action.import"),
    // 导出
    EXPORT(256, "action.export"),
    // 初审核 INITIAL_AUDIT
    FIRST_AUDIT(512, "action.first-audit"),
    // 复审
    LAST_AUDIT(1024, "action.last-audit"),
    // 撤销
    REVOKE(2048, "action.revoke"),
    // 打印
    PRINT(4096, "action.print"),
    // 恢复
    RECOVER(8192, "action.recover"),
    // 重置
    RESET(16384, "action.reset"),
    //  拒绝
    REJECT(32768, "action.reject");
    //    DISABLE( 8192,"禁用"),
    //    ENABLE( 16384,"启用"),
    //    DESTROY( 32768,"销毁"),
    //    REPLACE( 65536,"替换"),
    // 以下功能待完善
    // REVERT(8192, "恢复，还原")
    private final int code;
    private final String text;

    Action(int code, String text) {
        this.code = code;
        this.text = text;
    }

    public static Set<Action> parseSet(int val) {
        Set<Action> actions = new HashSet<>();
        for (Action action : Action.values()) {
            if ((action.code & val) == action.code) {
                actions.add(action);
            }
        }
        return actions;
    }

    public static Action parse(int val) {
        for (Action action : Action.values()) {
            if (action.code == val) {
                return action;
            }
        }
        throw new IllegalArgumentException("Invalid Action value: " + val);
    }

    public static Action of(String name) {
        return Enum.valueOf(Action.class, name);
    }

    @Override
    public Integer getValue() {
        return this.code;
    }

    @Override
    public String getLabel() {
        return this.text;
    }
}
